
//% color="#838be7" iconWidth=50 iconHeight=40
namespace UDP{
    //% block="UDP服务器收到的广播消息" blockType="hat"
    export function udp_server(parameter: any, block: any) {
        Generator.addEvent('udp_server', "handle_udp_message", "udp_client_message")

    }
    //% block="广播消息（服务器端）" blockType="reporter"
    export function udp_server_mess(parameter: any, block: any) {
        Generator.addCode("udp_client_message")

    }
    //% block="设置UDP服务器端口[PORT]" blockType="command"
    //% PORT.shadow="number"   PORT.defl=8800
    export function udp_serverIint(parameter: any, block: any) {
        let port = parameter.PORT.code
        Generator.addImport(`from UDPServer import UDPServer`)
        Generator.addCode(`udp_server = UDPServer()`)
        Generator.addCode(`udp_server.setPort(${port}) `)
        Generator.addCode(`udp_server.set_callback(handle_udp_message) `)

    }
    //% block="UDP服务器发送消息[MESS]" blockType="command"
    //% MESS.shadow="string"   MESS.defl="hello,I am server"
    export function udp_server_send(parameter: any, block: any) {
        let mess = parameter.MESS.code

        Generator.addCode(`udp_server.sendUdpMsg(${mess})`)

    }

    //% block="---"
    export function noteSep() {

    }

    //% block="UDP客户端收到的广播消息" blockType="hat"
    export function udp_client(parameter: any, block: any) {
        Generator.addEvent('udp_client', "udp_callback", "udp_server_message")

    }
    //% block="广播消息（客户端）" blockType="reporter"
    export function udp_client_mess(parameter: any, block: any) {
        Generator.addCode("udp_server_message")

    }
    //% block="设置UDP客户端连接到服务器IP[IP]端口[PORT]" blockType="command"
    //% IP.shadow="string"   IP.defl=192.168.4.1
    //% PORT.shadow="number"   PORT.defl=8800
    export function udp_client_Iint(parameter: any, block: any) {
        let ip = parameter.IP.code
        let port = parameter.PORT.code
        Generator.addImport(`from UDPClient import UDPClient`)
        Generator.addCode(`udp_client = UDPClient()`)
        Generator.addCode(`udp_client.connect_to_server(${ip}, ${port})`)
        Generator.addCode(`udp_client.set_callback(udp_callback)`)
    }
    //% block="UDP客户端发送到消息[MESS]" blockType="command"
    //% MESS.shadow="string"   MESS.defl="hello,I am client"
    export function udp_client_send(parameter: any, block: any) {
        let mess = parameter.MESS.code
        Generator.addCode(`udp_client.send_udp_msg(${mess})`)

    }


}
